package com.manli.api.service;

import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.List;
import com.manli.api.pojo.Result;
import com.manli.api.util.ResultUtil;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import com.manli.api.dao.PermissionDao;
import com.manli.api.dto.Permission;
import org.springframework.transaction.annotation.Transactional;


@Service("permissionService")
public class PermissionService {
    @Autowired
    PermissionDao permissionDao;

    
    public Result queryPage(Map<String, Object> params) {
        PageQuery<Permission> page = permissionDao.createQuery()
                .page(Long.parseLong(params.get("page").toString()),Long.parseLong(params.get("limit").toString()));
        return ResultUtil.success(page.getList(),page.getTotalRow());
    }

    
    public Result queryAll() {
        List<Permission> list = permissionDao.all();
        return ResultUtil.success(list);
    }

    
    public Result getById(Integer permissionId) {
        Permission permission = permissionDao.single(permissionId);
        return ResultUtil.success(permission);
    }

    @Transactional
    public Result save(Permission permission) {
        permission.setStatus(1);
        permissionDao.insert(permission);
        return ResultUtil.success();
    }

    @Transactional
    public Result updateById(Permission permission) {
        permissionDao.updateTemplateById(permission);
        return ResultUtil.success();
    }

    @Transactional
    public Result removeById(Integer permissionId) {
        permissionDao.deleteById(permissionId);
        return ResultUtil.success();
    }

    @Transactional
    public Result removeByIds(List<Integer> ids) {
        for (Integer id:ids) {
            permissionDao.executeUpdate("update permission set status = 0 where permission_id = ?",id);
        }
        return ResultUtil.success();
    }



}
